class ApiError(Exception):
    """API基础异常"""
    def __init__(self, message: str, code: int = 1000):
        super().__init__(message)
        self.code = code  # 错误代码（参考服务端XWebTrd.c的错误码）

class ConnectionError(ApiError):
    """连接相关异常"""
    def __init__(self, message: str, code: int = 2000):
        super().__init__(message, code)

class AuthError(ApiError):
    """认证失败异常"""
    def __init__(self, message: str, code: int = 3000):
        super().__init__(f"认证错误: {message}", code)

class LoginTimeoutError(AuthError):
    """登录超时异常"""
    def __init__(self, timeout: int):
        super().__init__(f"登录超时，等待时间: {timeout}秒", 3001)

class InvalidCredentialsError(AuthError):
    """无效凭证异常"""
    def __init__(self):
        super().__init__("用户名或密码错误", 3002)

class ApiRequestError(ApiError):
    """API请求失败异常"""
    def __init__(self, message: str, req_id: int, code: int = 4000):
        super().__init__(f"请求{req_id}失败: {message}", code)
        self.req_id = req_id

class RateLimitError(ApiError):
    """API调用频率超限"""
    def __init__(self, message: str = "请求频率超限", code: int = 5000):
        super().__init__(message, code)

class InvalidMessageFormat(ApiError):
    """消息格式错误"""
    def __init__(self, raw_data: str, code: int = 6000):
        super().__init__(f"无效的消息格式: {raw_data[:100]}", code)
        self.raw_data = raw_data

class InvalidOrderError(ApiError):
    """订单参数错误"""
    def __init__(self, field: str, message: str, code: int = 7000):
        super().__init__(f"订单字段[{field}]错误: {message}", code)
        self.field = field

class MarketAuthError(AuthError):
    """行情认证专用异常"""
    def __init__(self, code: str, message: str):
        super().__init__(f"行情认证失败 [{code}]: {message}", 3100) 